package com.hjl.cn.config.api;

import com.hjl.cn.vo.UsersVo;
import org.springframework.core.MethodParameter;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author jialin
 * @createTime 2020-09-14
 */
//处理参数
public class CurrentUserComplete implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //判断注解，判断拿到的参数的类是不是UsersVo
        return parameter.hasParameterAnnotation(CurrentUser.class) && parameter.getParameterType().isAssignableFrom(UsersVo.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        UsersVo usersVo = (UsersVo) nativeWebRequest.getAttribute("loginUserVo", RequestAttributes.SCOPE_REQUEST);
        if (!ObjectUtils.isEmpty(usersVo)) {
            return usersVo;
        }
        return null;
    }
}
